// uses jquery
// uses jquery.placeholder
// uses jquery.contextMenu
// uses jquery.overlay
// uses jquery.datepicker
// uses jquery.wysiwyg

var kavadenn = kavadenn || {};

kavadenn.CommunityApplication = function(community,settings){
  this.community = community;
  this.initializeUI();
}

kavadenn.CommunityApplication.prototype.initializeUI = function(){
  //HTML 5 placeholders (watermarks)
  $('input').placeholder();
  
  //launch search on enter
  $("#searchBox").bind("keypress",function(e){
    var code = (e.keyCode ? e.keyCode : e.which);
    if(code == 13) { //Enter keycode
      $("#searchForm").submit();
    }
  });
  
  // ** context menu for adding content
  $('#btnAddContent').contextMenu('#addContentContextMenu', { offsetY:5 });

  // ** editors
  $(".editor-launcher").overlay({color: '#ccc',top: 50});
  
  // ** datepicker for events
  $("#eventStartDate").datePicker();
  
  // ** wysiwyg
  $("#tipContentTxt").wysiwyg(
    {controls: {
      underline: { visible: false },indent: { visible: false },
      outdent: { visible: false }, separator02: { visible: false },
      subscript: { visible: false }, superscript: { visible: false },
      separator03: { visible: false }, insertImage: { visible: false },
      h1: { visible: false }, h2: { visible: false },
      h3: { visible: false }, h1mozilla: { visible: false },
      h2mozilla: { visible: false }, h3mozilla: { visible: false },
      separator06: { visible: false }
  }});
  
  //add tip 
  $("#tipAddBtn").click(this._bindCallback(this.addTip));
  
  //add link
  $("#linkAddBtn").click(this._bindCallback(this.addLink));
  
  //add event
  $("#eventAddBtn").click(this._bindCallback(this.addEvent));
}

kavadenn.CommunityApplication.prototype.addTip = function(){
  var community = this.community;
  var content = $("#tipContentTxt").val();
  if(content == "") return;
               
  $("#tipAddBtn").attr("disabled","disabled");
               
  $.post("/tips/add/",{"community":community,"content":content},
    function(d){
      $("#tipAddBtn").removeAttr("disabled");
      $("#tipContentTxt").wysiwyg('clear');
      var api = $("#tipsEditorLauncher").data("overlay"); 
      api.close();
      $("#wall").prepend(d.html);
    }
  );
}

kavadenn.CommunityApplication.prototype.addLink = function(){
  var community=this.community;
  var title = $("#linkTitleText").val();
  var url = $("#linkUrlText").val();

  $("#linkAddBtn").attr("disabled","disabled"); 
 
  $.post("/links/add/",{"community":community,"title":title,"url":url},
    function(d){
      $("#linkAddBtn").removeAttr("disabled");
      $("#linkTitleText").val("");
      $("#linkTitleUrl").val("");
 
      var api = $("#linksEditorLauncher").data("overlay"); 
      api.close();
 
      $("#wall").prepend(d.html);
    });
}

kavadenn.CommunityApplication.prototype.addEvent = function(){
  var community = this.community;
  var title = $("#eventTitleText").val();
  var place = $("#eventTitlePlace").val();
  var startDate = $("#eventStartDate").val();
  var endDate = startDate;
  var description = $("#eventDescriptionText").val();
               
  $("#eventAddBtn").attr("disabled","disabled");
   
  $.post("/events/add/",{"title":title,"description":description, 
                         "place":place,"community":community,
                         "start-date":startDate,"end-date":endDate},
          function(d){
            $("#eventAddBtn").removeAttr("disabled");
            var api = $("#eventsEditorLauncher").data("overlay"); 
            api.close();
            $("#wall").prepend(d.html);
          }
  );
}

kavadenn.CommunityApplication.prototype._bindCallback = function(callback) {
    //utility method to bind callback methods to the object instance
    var objSelf = this;
    return (function() { return (callback.apply(objSelf, arguments)); });
}
